<template>
  <el-card class="main-card">
    <div class="title">{{ this.$route.name }}</div>
    <div class="operation-container">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="openModel(null)"> 新增 </el-button>
      <el-button
        type="danger"
        size="small"
        icon="el-icon-delete"
        :disabled="linkIdList.length === 0"
        @click="deleteFlag = true">
        批量删除
      </el-button>
      <div style="margin-left: auto">
        <el-input
          v-model="keywords"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="请输入友链名"
          style="width: 200px"
          @keyup.enter.native="searchLinks" />
        <el-button type="primary" size="small" icon="el-icon-search" style="margin-left: 1rem" @click="searchLinks">
          搜索
        </el-button>
      </div>
    </div>
    <el-table border :data="linkList" @selection-change="selectionChange" v-loading="loading">
      <el-table-column type="selection" width="55" align="center" fixed='left'/>
      <el-table-column prop="linkAvatar" label="友链头像" align="center" width="100">
        <template slot-scope="scope">
          <img :src="scope.row.linkAvatar" width="40" height="40"  alt='友链头像' @error='handleErrorAvatar'/>
        </template>
      </el-table-column>
      <el-table-column prop="linkName" label="友链名称" align="center" />
      <el-table-column prop="linkAddress" label="友链地址" width='240' align="center">
        <template slot-scope='scope'>
          <el-link type="primary" @click='copyTable(scope.row.linkAddress)'>
            {{ scope.row.linkAddress }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="linkIntro" label="友链介绍" width='180' align="center" show-overflow-tooltip/>
      <el-table-column prop="linkTag" label="友链标签" width='100' align="center">
        <template slot-scope='scope'>
          <el-tag v-if='scope.row.linkTag' style="margin-right: 0.2rem; margin-top: 0.2rem">
            {{ scope.row.linkTag}}
          </el-tag>
          <el-tag v-else style="margin-right: 0.2rem; margin-top: 0.2rem" type="warning">
            暂无标签
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isRecommend" label="友链推荐" align="center">
        <template slot-scope='scope'>
          <el-switch
            v-model='scope.row.isRecommend'
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="1"
            :inactive-value="0"
            @change='changeIsRecommend(scope.row)'>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop='isDelete' label='逻辑删除' align='center'>
        <template slot-scope='scope'>
          <el-switch
            v-model='scope.row.isDelete'
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="1"
            :inactive-value="0"
            @change='changeIsDelete(scope.row)'>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="140" align="center">
        <template slot-scope="scope">
          <i class="el-icon-time" style="margin-right: 5px" />
          {{ scope.row.createTime | date }}
        </template>
      </el-table-column>
      <el-table-column prop='siteScreenshot' width='180' label='站点截图' align='center'show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" align="center" width="160" fixed='right'>
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="openModel(scope.row)"> 编辑 </el-button>
          <el-popconfirm title="确定删除吗？" style="margin-left: 1rem" @confirm="deleteLink(scope.row.id)">
            <el-button size="mini" type="danger" slot="reference"> 删除 </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination-container"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page="current"
      :page-size="size"
      :total="count"
      :page-sizes="[10, 20]"
      layout="total, sizes, prev, pager, next, jumper" />
    <el-dialog :visible.sync="deleteFlag" width="30%">
      <div class="dialog-title-container" slot="title"><i class="el-icon-warning" style="color: #ff9900" />提示</div>
      <div style="font-size: 1rem">是否删除选中项？</div>
      <div slot="footer">
        <el-button @click="deleteFlag = false">取 消</el-button>
        <el-button type="primary" @click="deleteLink(null)"> 确 定 </el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="addOrEdit" width="30%">
      <div class="dialog-title-container" slot="title" ref="linkTitle" />
      <el-form label-width="80px" size="medium" :model="linkForm">
        <el-form-item label="友链名称">
          <el-input style="width: 250px" v-model="linkForm.linkName" />
        </el-form-item>
        <el-form-item label="友链头像">
          <el-input style="width: 250px" v-model="linkForm.linkAvatar" />
        </el-form-item>
        <el-form-item label="友链地址">
          <el-input style="width: 250px" v-model="linkForm.linkAddress" />
        </el-form-item>
        <el-form-item label="友链介绍">
          <el-input style="width: 250px" v-model="linkForm.linkIntro" />
        </el-form-item>
        <el-form-item label="友链标签">
          <el-input style="width: 250px" v-model="linkForm.linkTag" />
        </el-form-item>
        <el-form-item label="友链分类">
          <el-select v-model='linkForm.categoryId' placeholder='请选择类型'>
            <el-option v-for='item in linkCategoryList' :key='item.id' :label='item.name' :value='item.id' />
          </el-select>
        </el-form-item>
        <el-form-item label="站点截图">
          <el-input style="width: 250px" v-model="linkForm.siteScreenshot" />
        </el-form-item>
      </el-form>
      <div class="friend-footer">
        <el-button @click="addOrEdit = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditCategory"> 确 定 </el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { handleErrorAvatar } from '@/util/cravatarUtil'
import { copyTable } from '@/util/copyUtil'

export default {
  created() {
    this.current = this.$store.state.pageState.friendLink
    this.listLinks()
    this.getCategoryList()
  },
  data: function () {
    return {
      loading: true,
      deleteFlag: false,
      addOrEdit: false,
      linkIdList: [],
      linkList: [],
      linkCategoryList:[],
      linkForm: {
        id: null,
        linkName: '',
        linkAvatar: '',
        linkIntro: '',
        linkAddress: '',
        isRecommend: '',
        isDelete: '',
        status: '',
        siteScreenshot: '',
        linkTag: ''
      },
      keywords: null,
      current: 1,
      size: 10,
      count: 0
    }
  },
  methods: {
    copyTable,
    handleErrorAvatar,
    getCategoryList(){
      this.axios
        .get('/api/links/category').then(({ data })=>{
          console.log(data.data)
        this.linkCategoryList=data.data
      })
    },
    changeIsRecommend(link){
      this.axios
        .put('/api/admin/links/recommend',{
          id:link.id,
          isRecommend: link.isRecommend
        }).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: '成功',
            message: '推荐成功'
          })
        } else {
          this.$notify.error({
            title: '推荐失败',
            message: data.message
          })
        }
        this.remove = false
      })
    },
    changeIsDelete(link){
      this.axios
        .put('/api/admin/links/is-delete',{
          id:link.id,
          isDelete: link.isDelete
        }).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: '成功',
            message: '逻辑删除成功'
          })
        } else {
          this.$notify.error({
            title: '逻辑删除失败',
            message: data.message
          })
        }
        this.remove = false
      })
    },
    selectionChange(linkList) {
      this.linkIdList = []
      linkList.forEach((item) => {
        this.linkIdList.push(item.id)
      })
    },
    searchLinks() {
      this.current = 1
      this.listLinks()
    },
    sizeChange(size) {
      this.size = size
      this.listLinks()
    },
    currentChange(current) {
      this.current = current
      this.$store.commit('updateFriendLinkPageState', current)
      this.listLinks()
    },
    deleteLink(id) {
      var param = {}
      if (id == null) {
        param = { data: this.linkIdList }
      } else {
        param = { data: [id] }
      }
      this.axios.delete('/api/admin/links', param).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: '成功',
            message: data.message
          })
          this.listLinks()
        } else {
          this.$notify.error({
            title: '失败',
            message: data.message
          })
        }
        this.deleteFlag = false
      })
    },
    openModel(link) {
      if (link != null) {
        this.linkForm = JSON.parse(JSON.stringify(link))
        this.$refs.linkTitle.innerHTML = '修改友链'
      } else {
        this.linkForm.id = null
        this.linkForm.linkName = ''
        this.linkForm.linkAvatar = ''
        this.linkForm.linkIntro = ''
        this.linkForm.linkAddress = ''
        this.$refs.linkTitle.innerHTML = '添加友链'
      }
      this.addOrEdit = true
    },
    addOrEditCategory() {
      if (this.linkForm.linkName.trim() == '') {
        this.$message.error('友链名不能为空')
        return false
      }
      if (this.linkForm.linkAvatar.trim() == '') {
        this.$message.error('友链头像不能为空')
        return false
      }
      if (this.linkForm.linkIntro.trim() == '') {
        this.$message.error('友链介绍不能为空')
        return false
      }
      if (this.linkForm.linkAddress.trim() == '') {
        this.$message.error('友链地址不能为空')
        return false
      }
      this.axios.post('/api/admin/links', this.linkForm).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: '成功',
            message: data.message
          })
          this.listLinks()
        } else {
          this.$notify.error({
            title: '失败',
            message: data.message
          })
        }
        this.addOrEdit = false
      })
    },
    listLinks() {
      this.axios
        .get('/api/admin/links', {
          params: {
            current: this.current,
            size: this.size,
            keywords: this.keywords
          }
        })
        .then(({ data }) => {
          this.linkList = data.data.records
          this.count = data.data.count
          this.loading = false
        })
    }
  }
}
</script>
<style scoped>
.friend-footer{
    display: flex;
    justify-content: center;
}
</style>
